import inherits from 'inherits';
import Modeler from 'bpmn-js/lib/Modeler';
import idProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/IdProps';
import nameProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/nameProps';
import processProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/processProps';
import versionTag from 'bpmn-js-properties-panel/lib/provider/camunda/parts/VersionTagProps';
import executableProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/ExecutableProps';
import elementTemplateChooserProps from 'bpmn-js-properties-panel/lib/provider/camunda/element-templates/parts/ChooserProps';
import serviceTaskDelegateProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/ServiceTaskDelegateProps';
import conditionalProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/ConditionalProps';

import PropertiesActivator from 'bpmn-js-properties-panel/lib/PropertiesActivator';

import { is, getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';

const PROCESS_KEY_HINT = 'This maps to the process definition key.';
const TASK_KEY_HINT = 'This maps to the task definition key.';

function getIdOptions(element) {
  if (is(element, 'bpmn:Participant')) {
    return { id: 'participant-id', label: 'Participant Id' };
  }

  if (is(element, 'bpmn:Process')) {
    return { description: PROCESS_KEY_HINT };
  }

  if (is(element, 'bpmn:UserTask')) {
    return { description: TASK_KEY_HINT };
  }
}

function getNameOptions(element) {
  if (is(element, 'bpmn:Participant')) {
    return { id: 'participant-name', label: 'Participant Name' };
  }
}

function getProcessOptions(element) {
  if (is(element, 'bpmn:Participant')) {
    return { processIdDescription: PROCESS_KEY_HINT };
  }
}

function createGeneralTabGroups(
  element,
  canvas,
  bpmnFactory,
  elementRegistry,
  elementTemplates,
  translate,
) {
  // refer to target element for external labels
  const _element = element.labelTarget || element;

  const generalGroup = {
    id: 'general',
    label: translate('General'),
    entries: [],
  };

  idProps(generalGroup, _element, translate, getIdOptions(_element));
  nameProps(generalGroup, _element, bpmnFactory, canvas, translate, getNameOptions(_element));
  processProps(generalGroup, _element, translate, getProcessOptions(_element));

  const detailsGroup = {
    id: 'details',
    label: translate('Details'),
    entries: [],
  };
  conditionalProps(detailsGroup, _element, bpmnFactory, translate);

  const groups = [];
  groups.push(generalGroup, detailsGroup);

  return groups;
}

/**
 * A properties provider for Camunda related properties.
 *
 * @param {BpmnFactory} bpmnFactory
 * @param {Canvas} canvas
 * @param {ElementRegistry} elementRegistry
 * @param {ElementTemplates} elementTemplates
 * @param {EventBus} eventBus
 * @param {Modeling} modeling
 * @param {Replace} replace
 * @param {Selection} selection
 * @param {Translate} translate
 */
function ControlPropertiesProvider(
  bpmnFactory,
  canvas,
  commandStack,
  elementRegistry,
  elementTemplates,
  eventBus,
  modeling,
  replace,
  selection,
  translate,
) {
  PropertiesActivator.call(this, eventBus);

  this.getTabs = function (element) {
    const generalTab = {
      id: 'general',
      label: translate('General'),
      groups: createGeneralTabGroups(
        element,
        canvas,
        bpmnFactory,
        elementRegistry,
        elementTemplates,
        translate,
      ),
    };

    return [generalTab];
  };
}

ControlPropertiesProvider.$inject = [
  'bpmnFactory',
  'canvas',
  'commandStack',
  'elementRegistry',
  'elementTemplates',
  'eventBus',
  'modeling',
  'replace',
  'selection',
  'translate',
];

inherits(ControlPropertiesProvider, PropertiesActivator);

export default ControlPropertiesProvider;
